package com.csy.property.community.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csy.property.community.domain.AjBuilding;
import com.csy.property.community.domain.AjUnit;
import com.csy.property.community.domain.vo.RoomTree;
import com.csy.property.community.mapper.AjBuildingMapper;
import com.csy.property.community.mapper.AjCommunityMapper;
import com.csy.property.community.mapper.AjUnitMapper;
import com.csy.property.community.service.AjUnitService;
import com.csy.property.entity.AjCommunityArea;
import com.csy.property.mapper.AjCommunityAreaMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Auther: sunxx
 * @Date: 2024/4/22 - 04 - 22 - 18:26
 * @Description: com.sxx.ajcommunity.community.service.impl
 * @version: 1.0
 */
@Service
public class AjUnitServiceImpl extends ServiceImpl<AjUnitMapper,AjUnit> implements AjUnitService {
    @Resource
    private AjUnitMapper ajUnitMapper;
    @Resource
    private AjCommunityMapper ajCommunityMapper;
    @Resource
    private AjCommunityAreaMapper ajCommunityAreaMapper;
    @Resource
    private AjBuildingMapper ajBuildingMapper;
    private final String CODE_PREFIX = "UNIT_";
    //查询小区列表集合
    @Override
    public List<AjUnit> selectUnitList(AjUnit ajUnit) {
        return ajUnitMapper.selectUnitList(ajUnit);
    }

    @Override
    public AjUnit getById(Long unitId) {
        return ajUnitMapper.selectById(unitId);
    }

    @Override
    public int add(AjUnit ajUnit) {

        AjBuilding building = ajBuildingMapper.selectById(ajUnit.getBuildingId());
        if (ObjectUtils.isNull(building)){
            return 0;
        }
        try {
            ajUnit.setCommunityId(ajUnit.getCommunityId());
            ajUnit.setBuildingId(building.getBuildingId());
        }catch (Exception e){
            e.printStackTrace();
        }
        ajUnit.setUnitCode(CODE_PREFIX+System.currentTimeMillis());
        ajUnit.setUnitId(IdUtil.getSnowflake(1,1).nextId());
        Date date = new Date();
        ajUnit.setCreateTime(date);

        return ajUnitMapper.insert(ajUnit);
    }

    @Override
    public int updateUnit(AjUnit ajUnit) {
        ajUnit.setUpdateTime(new Date());
        return ajUnitMapper.myUpdateById(ajUnit);
    }

    @Override
    public int deleteAjUnit(Long[] ids) {
        return ajUnitMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<RoomTree> queryPullDownRoom(AjUnit ajUnit) {

        List<RoomTree> list = new ArrayList<>();
        List<AjBuilding> buildings = ajBuildingMapper.queryPullDown(ajUnit.getCommunityId());

        for (AjBuilding ajBuilding :buildings) {
            RoomTree roomTree = new RoomTree();
            roomTree.setName(ajBuilding.getBuildingName());
            roomTree.setCode(ajBuilding.getBuildingId());
            AjUnit unit = new AjUnit();
            unit.setBuildingId(ajBuilding.getBuildingId());
            unit.setCommunityId(ajUnit.getCommunityId());
            List<AjUnit> ajUnits = ajUnitMapper.selectUnitList(unit);
            List<RoomTree> units = new ArrayList<>();
            for (AjUnit u:ajUnits){
                RoomTree bu = new RoomTree();
                bu.setName(u.getUnitName());
                bu.setCode(u.getUnitId());
                units.add(bu);
            }
            roomTree.setChildren(units);
            list.add(roomTree);
        }
        return list;
    }

    @Override
    public List<RoomTree> queryPullAreaBuildUnit(AjUnit ajUnit) {
        Long communityId = ajUnit.getCommunityId();
        List<AjCommunityArea> areaList = ajCommunityAreaMapper.getAreaList(new AjCommunityArea().setCommunityId(communityId));
        ArrayList<RoomTree> areaBuildUnitTree = new ArrayList<>();
        if (areaList.size() == 0){
            //area对象
            RoomTree areaTree = new RoomTree();
            areaTree.setName("无");
            areaTree.setCode(0l);
            //building集合作为child集合
//            System.out.println("area为空-----");
            List<RoomTree> buildingtrees = new ArrayList<>();
            List<AjBuilding> list = ajBuildingMapper.queryPullDown(communityId);
            for (AjBuilding building:list){
                RoomTree buildingTree = new RoomTree();
                buildingTree.setCode(building.getBuildingId());
                buildingTree.setName(building.getBuildingName());
                //unit集合
                List<RoomTree> unitTree = new ArrayList<>();
                AjUnit unit = new AjUnit();
                unit.setBuildingId(building.getBuildingId());
                //根据查到的楼栋查单元
                List<AjUnit> ajUnits = ajUnitMapper.selectUnitList(unit);
                for (AjUnit u:ajUnits){
                    RoomTree ajunit = new RoomTree();
                    ajunit.setCode(u.getUnitId());
                    ajunit.setName(u.getUnitName());
                    //把每个单元封装放到unit集合中
                    unitTree.add(ajunit);
                }
                //封装的buildingTree对象把unit集合放进去
                buildingTree.setChildren(unitTree);
                //封装的buildingTree对象放入building集合
                buildingtrees.add(buildingTree);
            }
            areaTree.setChildren(buildingtrees);
            areaBuildUnitTree.add(areaTree);
        }else {
//            System.out.println("area不为空-----");
            for (AjCommunityArea ajCommunityArea : areaList) {
                //area对象
                RoomTree areaTree = new RoomTree();
                areaTree.setName(ajCommunityArea.getCommunityArea());
                areaTree.setCode(ajCommunityArea.getId());
                //根据area对象areaId查building
                AjBuilding building = new AjBuilding();
                building.setCommunityAreaId(ajCommunityArea.getId());
                List<AjBuilding> ajBuildings = ajBuildingMapper.selectBuildingList(building);
                //building集合作为child集合
                List<RoomTree> buildingtrees = new ArrayList<>();
                for (AjBuilding ajBuild : ajBuildings) {
                    //building对象
                    RoomTree buildTree = new RoomTree();
                    buildTree.setCode(ajBuild.getBuildingId());
                    buildTree.setName(ajBuild.getBuildingName());
                    //unit集合
                    List<RoomTree> unitTree = new ArrayList<>();
                    AjUnit unit = new AjUnit();
                    unit.setBuildingId(ajBuild.getBuildingId());
                    //根据查到的楼栋查单元
                    List<AjUnit> ajUnits = ajUnitMapper.selectUnitList(unit);
                    for (AjUnit u:ajUnits){
                        RoomTree ajunit = new RoomTree();
                        ajunit.setCode(u.getUnitId());
                        ajunit.setName(u.getUnitName());
                        //把每个单元封装放到unit集合中
                        unitTree.add(ajunit);
                    }
                    //封装的buildingTree对象把unit集合放进去
                    buildTree.setChildren(unitTree);
                    //封装的buildingTree对象放入building集合
                    buildingtrees.add(buildTree);
                }
                areaTree.setChildren(buildingtrees);
                areaBuildUnitTree.add(areaTree);
            }
        }
        return areaBuildUnitTree;
    }
}
